from django.shortcuts import redirect
from app01.models import ForumReviews, UserInfo
from django.shortcuts import render, HttpResponse


# def userlogin(request):
#     if request.method == "GET":  # 首次登录页面 get
#         return render(request, 'login.html')
#     else:
#         # print(request.POST)#如果是post请求 获取用户提交的数据
#         username = request.POST.get("user")
#         password = request.POST.get("pwd")
#         if username == 'root' and password == "123":
#             # return HttpResponse("登录成功")
#             return redirect("http://www.baidu.com/")
#         else:
#             # return HttpResponse("登录失败: 用户名或密码有误 ")
#             return render(request, 'login.html', {"error": "用户名或密码错误"})


def orm(request):
    # 添加数据
    # ForumReviews.objects.create(contents="测试2")
    # UserInfo.objects.create(username="Alan", password="123")
    # UserInfo.objects.create(username="NPC",password="456")
    # 删除数据 先筛选再删除
    # UserInfo.objects.filter(id=1).delete()
    # ForumReviews.objects.all().delete()
    # UserInfo.objects.all().delete()
    # 获取数据  得到的是列表（queryset）即：datalist=[行，行，行]
    # datalist = UserInfo.objects.all()
    # print(datalist)
    # for obj in datalist:
    #     print(obj.id,obj.name,obj.password)
    # datalist = UserInfo.objects.filter(id=3)
    # print(datalist)
    # 更新数据
    # UserInfo.objects.all().update(password=123)
    # UserInfo.objects.filter(id=4).update(password=999)
    return HttpResponse("成功")
